<?php

namespace App\Http\Middleware;

use App\Lib\ErrorCode;
use Closure;
use Illuminate\Support\Facades\Redis;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->hasHeader('token') && !$request->has('token')){
            return response()->json(["code"=>ErrorCode::FORBIDDEN,"msg"=>"请登录后再访问"],200,[],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        }
        $token = $request->header('token')?:$request->input('token');
        if (!Redis::connection('default')->exists($token)){
            return response()->json(["code"=>ErrorCode::FORBIDDEN,"msg"=>"登录信息已过期,请重新登录"],200,[],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        }
        return $next($request);
    }
}
